﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto;
using WCS_Entity.Dto.UserDto;
using WCS_Service.Interface;

namespace WCS.Controllers
{
    /// <summary>
    /// 角色管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;
        public RoleController(IRoleService roleService) 
        {
            _roleService = roleService;
        }

        #region 角色管理
        /// <summary>
        /// 查询指定角色信息
        /// </summary>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryRuleAsync(int ruleId)
        {
            var result = new WebApiCallBack();
            var ruleDto = await _roleService.QueryRuleAsync(ruleId);
            if (ruleDto is null)
            {
                result.status = false;
                result.msg = "查询角色信息不存在";
                return result;
            }
            result.data = ruleDto;
            return result;
        }
        /// <summary>
        /// 创建角色信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreateRuleAsync([FromBody] CreateRuleDto dto)
        {
            var result = new WebApiCallBack();

            var ruleDto = await _roleService.CreateRuleAsync(dto);
            if (ruleDto is null)
            { 
                result.status = false;
                result.msg = "角色创建失败";
                return result;
            }
            result.data = ruleDto;
            return result;
        }
        /// <summary>
        /// 删除指定的角色信息
        /// </summary>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteRuleAsync(int ruleId)
        {
            var result = new WebApiCallBack();
            result.status = await _roleService.DeleteByIdAsync(ruleId);
            result.msg = result.status ? "角色删除成功" : "角色删除失败";

            return result;

        }
        /// <summary>
        /// 查询所有角色信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllRuleAsync()
        {
            var result = new WebApiCallBack();
            var ruleInfos = await _roleService.QueryAllRuleAsync();
            if (ruleInfos is null)
            {
                result.status = false;
                result.msg = "查询所有角色信息失败";
                return result;
            }
            result.data = ruleInfos;
            return result;
        }
        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateRuleAsync([FromBody] UpdateRuleDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _roleService.UpdateRuleAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "更新的角色信息不存在";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        #endregion

    }
}
